<template>
  <view 
    class="book-item" 
    @click="handleClick"
    :class="{'book-item--horizontal': direction === 'horizontal', 'book-item--vertical': direction === 'vertical'}"
  >
    <!-- 图书封面 -->
    <view class="book-item__cover">
      <image 
        class="book-item__cover-image" 
        :src="book.cover" 
        mode="aspectFill"
        :style="{width: coverSize + 'px', height: coverSize * 1.33 + 'px'}"
      ></image>
      <!-- 角标/标签 -->
      <slot name="badge"></slot>
    </view>
    
    <!-- 图书信息 -->
    <view class="book-item__info">
      <text class="book-item__title" :style="{fontSize: titleSize + 'px'}">{{book.title}}</text>
      <text class="book-item__author">{{book.author || '未知作者'}}</text>
      
      <!-- 底部附加信息 -->
      <view class="book-item__footer" v-if="$slots.footer || showFooter">
        <slot name="footer">
          <text class="book-item__price" v-if="book.price">¥{{book.price}}</text>
          <text class="book-item__publisher" v-if="book.publisher">{{book.publisher}}</text>
        </slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'BookItem',
  props: {
    book: {
      type: Object,
      required: true,
      default: () => ({
        cover: '',
        title: '未知图书',
        author: '',
        publisher: '',
        price: null
      })
    },
    // 排列方向：horizontal(水平) | vertical(垂直)
    direction: {
      type: String,
      default: 'horizontal',
      validator: value => ['horizontal', 'vertical'].includes(value)
    },
    // 封面尺寸(宽度)
    coverSize: {
      type: Number,
      default: 75
    },
    // 标题字体大小
    titleSize: {
      type: Number,
      default: 14
    },
    // 是否显示默认底部信息
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleClick() {
      this.$emit('click', this.book)
    }
  }
}
</script>

<style lang="scss" scoped>
.book-item {
  display: flex;
  background-color: #fff;
  transition: all 0.2s ease;
  
  &:active {
    opacity: 0.8;
    transform: scale(0.98);
  }
  
  &--horizontal {
    flex-direction: row;
    padding: 12rpx;
    border-bottom: 1rpx solid #f5f5f5;
    
    .book-item__info {
      margin-left: 20rpx;
      flex: 1;
    }
  }
  
  &--vertical {
    flex-direction: column;
    align-items: center;
    padding: 16rpx;
    
    .book-item__info {
      margin-top: 12rpx;
      width: 100%;
      text-align: center;
    }
  }
  
  &__cover {
    position: relative;
    
    &-image {
      border-radius: 4rpx;
      box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
    }
  }
  
  &__info {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  
  &__title {
    font-weight: 500;
    color: #333;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  &__author {
    font-size: 12px;
    color: #999;
    margin-top: 6rpx;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  &__footer {
    margin-top: 12rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  &__price {
    font-size: 14px;
    color: #f56c6c;
    font-weight: bold;
  }
  
  &__publisher {
    font-size: 12px;
    color: #999;
  }
}
</style>